package com.ayf.payment.game.api.config;

import com.ayf.payment.game.api.cache.redis.RedisCacheUtil;
import com.ayf.payment.game.api.dto.ProxyGroupDTO;
import com.ayf.payment.game.api.dto.platform.*;
import com.ayf.payment.game.api.entity.ProxyGroup;
import com.ayf.payment.game.api.entity.ProxyGroupRate;
import com.ayf.payment.game.api.service.ProxyGroupService;
import com.ayf.payment.game.api.service.platform.MerchantSettleRateService;
import com.ayf.payment.game.api.service.platform.PaymentChannelService;
import com.ayf.payment.game.api.service.platform.SettleConfigService;
import com.swwx.charm.commons.lang.utils.LogPortal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 *
 * @author wwl
 * @date 2020/3/19 11:43
 */
@Component
public class SystemInitialization {

    private SettleConfigDTO settleConfigDTO=null;

    @Autowired
    private PaymentChannelService paymentChannelService;

    @Autowired
    private MerchantSettleRateService merchantSettleRateService;

    @Autowired
    private ProxyGroupService proxyGroupService;
    @Autowired
    private SettleConfigService settleConfigService;
    @Autowired
    private RedisCacheUtil redisCacheUtil;
    //充值通道
    private static final String SYS_CACHE_CHANNELMAP="AYF_SYS_CACHE_CHANNELMAP";
    //充值通道 充值产品
    private static final  String SYS_CACHE_PRODUCTMAP="AYF_SYS_CACHE_PRODUCTMAP";
   // 充值产品费率
    private static final  String SYS_CACHE_SETTLERATEMAP="AYF_SYS_CACHE_SETTLERATEMAP";
    // 代理商分组产品
    private static final  String SYS_CACHE_PROXYGROUPMAP="AYF_SYS_CACHE_PROXYGROUPMAP";
    // 代理商分组产品费率
    private static final  String SYS_CACHE_PROXYGROUPRATEMAP="AYF_SYS_CACHE_PROXYGROUPRATEMAP";
    //系统配制
    private static final  String SYS_CACHE_SETTLECONFIGDTO="AYF_SYS_CACHE_SETTLECONFIGDTO";

    @PostConstruct
    public void init(){
        InitializationData("初始化");
    }

    public void InitializationData(String msg){
        LogPortal.info("InitializationData Start 初始化缓存--{}",msg);
        initChannel();
        initSettle();
        initProxyGroup();
        initSettleConfig();
        LogPortal.info("InitializationData End 初始化缓存--{}",msg);
    }

    public void initProxyGroup(){
        List<ProxyGroup> proxyList=proxyGroupService.selectAllGroupInfo();
        Map<Integer, Map<Integer,BigDecimal>> proxyGroupSettleRateMap=new HashMap<>();

        Map<Integer, ProxyGroupDTO> proxyGroupMap=new HashMap<>();
        for(ProxyGroup proxyGroup:proxyList){
            ProxyGroupDTO dto=new ProxyGroupDTO();
            dto.setId(proxyGroup.getId());
            dto.setDefaults(proxyGroup.getDefaults());
            dto.setGroupName(proxyGroup.getGroupName());
            dto.setMerchantId(proxyGroup.getMerchantId());
            dto.setMerchantNum(proxyGroup.getMerchantNum());
            dto.setCreateTime(proxyGroup.getCreateTime());

            Map<Integer,BigDecimal> map=new HashMap<>();
            for(ProxyGroupRate rate:proxyGroup.getGroupRateList()){
                map.put(rate.getPayProductId(),rate.getPaymentRate());
            }
            proxyGroupSettleRateMap.put(proxyGroup.getId(),map);
            proxyGroupMap.put(proxyGroup.getId(),dto);
        }
        redisCacheUtil.setObjectToCache(SYS_CACHE_PROXYGROUPRATEMAP,proxyGroupSettleRateMap);
        redisCacheUtil.setObjectToCache(SYS_CACHE_PROXYGROUPMAP,proxyGroupMap);
    }

    public void initSettle(){
        Map<Integer, Map<Integer,BigDecimal>> settleRateMap=new HashMap<>();
        List<MerchantSettleRateDTO> list= merchantSettleRateService.selectAll(null);

        for(MerchantSettleRateDTO dto:list){
            Map<Integer,BigDecimal> map=new HashMap<>();
            for(MerchantSettleRateConfigDTO configDTO:dto.getConfigList()){
                map.put(configDTO.getPayProductId(),configDTO.getPaymentRate());
            }
            settleRateMap.put(dto.getId(),map);
        }
        redisCacheUtil.setObjectToCache(SYS_CACHE_SETTLERATEMAP,settleRateMap);
    }

    public void initChannel(){
        Map<Integer, PaymentChannelDTO> channelMap=new HashMap<>();
        Map<Integer, MerchantPaymentProductDTO> productMap=new HashMap<>();
        LogPortal.info("initChannel Start 初始化支付通道产品缓存");
        List<PaymentChannelDTO> channelList=paymentChannelService.selectPaymentChanne();
        for(PaymentChannelDTO dto:channelList){
            channelMap.put(dto.getId(),dto);
            List<MerchantPaymentProductDTO> porductList=paymentChannelService.selectMerchantPaymentProduct(dto.getId());
            for(MerchantPaymentProductDTO pdto:porductList){
                productMap.put(pdto.getProductId(),pdto);
            }
        }
        redisCacheUtil.setObjectToCache(SYS_CACHE_CHANNELMAP,channelMap);
        redisCacheUtil.setObjectToCache(SYS_CACHE_PRODUCTMAP,productMap);
        LogPortal.info("initChannel End 初始化支付通道产品缓存");
    }

    public void initSettleConfig(){
        redisCacheUtil.delete(SYS_CACHE_SETTLECONFIGDTO);
        settleConfigDTO=settleConfigService.getSettleConfig();
        redisCacheUtil.setObjectToCache(SYS_CACHE_SETTLECONFIGDTO,settleConfigDTO);
    }

    public  Map<Integer, MerchantPaymentProductDTO> productMap() {
        Map<Integer, MerchantPaymentProductDTO> map=redisCacheUtil.getObjectFromCache(SYS_CACHE_PRODUCTMAP);
        if(null ==map){
            initChannel();
            map=redisCacheUtil.getObjectFromCache(SYS_CACHE_PRODUCTMAP);
        }
        return map;
    }

    public MerchantPaymentProductDTO productDTO(Integer pid) {
        MerchantPaymentProductDTO productDTO= productMap().get(pid);
        if(null==productDTO){
            productDTO=  paymentChannelService.getMerchantPaymentProductByPId(pid);
        }
        return productDTO;
    }

    public PaymentChannelDTO channelDTO(Integer cId) {
        PaymentChannelDTO paymentChannelDTO=channelMap().get(cId);
        if(null ==paymentChannelDTO){
            paymentChannelDTO=paymentChannelService.getPaymentChanneByCId(cId);
        }
        return paymentChannelDTO;
    }

    public  Map<Integer, PaymentChannelDTO> channelMap() {
        Map<Integer, PaymentChannelDTO> map=redisCacheUtil.getObjectFromCache(SYS_CACHE_CHANNELMAP);
        if(null ==map){
            initChannel();
            map=redisCacheUtil.getObjectFromCache(SYS_CACHE_CHANNELMAP);
        }
        return map;
    }

    public  Map<Integer, Map<Integer, BigDecimal>> settleRateMap() {
        Map<Integer, Map<Integer, BigDecimal>> map=redisCacheUtil.getObjectFromCache(SYS_CACHE_SETTLERATEMAP);
        if(null==map){
            initSettle();
            map=redisCacheUtil.getObjectFromCache(SYS_CACHE_SETTLERATEMAP);
        }
        return map;
    }

    public  Map<Integer, Map<Integer, BigDecimal>> proxyGroupSettleRateMap() {
        Map<Integer, Map<Integer, BigDecimal>> map=redisCacheUtil.getObjectFromCache(SYS_CACHE_PROXYGROUPRATEMAP);
        if(null==map){
            initProxyGroup();
            map=redisCacheUtil.getObjectFromCache(SYS_CACHE_PROXYGROUPRATEMAP);
        }
        return map;
    }

    public  Map<Integer, ProxyGroupDTO> proxyGroupMap() {
        Map<Integer, ProxyGroupDTO> map=redisCacheUtil.getObjectFromCache(SYS_CACHE_PROXYGROUPMAP);
        if(null==map){
            initProxyGroup();
            map=  redisCacheUtil.getObjectFromCache(SYS_CACHE_PROXYGROUPMAP);
        }
        return map;
    }


    public SettleConfigDTO settleConfigDTO() {
        return redisCacheUtil.getObjectFromCache(SYS_CACHE_SETTLECONFIGDTO);

    }

}
